<?php

class sfPropelRevisitedGeneratorRounder
{
  /**
   * Return the number of decimals used to round values
   */
  static public function getDecimals()
  {
    return sfPropelRevisitedGeneratorConfiguration::getNumberOfDecimals();
  }

  /**
   * Redondea un valor a una cantidad de decimales fijada en el app.yml
   */
  static public function round($number, $decimals = null)
  {
    $decimals = is_null($decimals)? self::getDecimals() : $decimals;

    if (is_numeric($number) && $decimals !== false)
    {
      $operator     = pow(10, $decimals);
      $value        = $number*$operator;
      $floor_value  = floor($value);

      if ($value-$floor_value >= 0.5)
      {
        $value = ceil($value);
      }
      else
      {
        $value = floor($value);
      }

      return $value/$operator;
    }
    return null;
  }

  static public function format($number)
  {
    if (is_numeric($number))
    {
      return number_format(self::round($number), self::getDecimals(), ",", ".");
    }
    return null;
  }
}
